package servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;

import bean.Customer;
import common.base.BaseServlet;
import common.util.CustomBeanUtils;
import common.util.UUIDUtils;
import service.UserService;
import service.impl.UserServiceImpl;


@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
   
	public String registUi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		return "/user/regist.jsp";
	}
	
	public String loginUi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		return "/user/login.jsp";
	}
	
	//注册
	public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, FileUploadException {
		//获得请求参数信息
		Map<String, String[]> parameterMap = request.getParameterMap();	
		Customer c = CustomBeanUtils.populate(Customer.class, parameterMap);
		c.setCustomerId(UUIDUtils.getId());
			  
		//调用service执行注册
		UserService service = new UserServiceImpl();
		boolean success = service.regist(c);
			  
		//成功，跳转到登录画面
		if (success) {
		//向session域中写入用户名
			request.getSession().setAttribute("usname", c.getcName());
		//重定向
			response.sendRedirect(request.getContextPath() + "/UserServlet?action=loginUi");
			  		} else {
			response.sendRedirect(request.getContextPath() + "/UserServlet?action=registUi");
			  		}
		 	return null;
		}
			
			//用户名判断
			public String usnameExit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
				String result = "";
				
				String usname = request.getParameter("usname");
				//调用Service判断判断用户是否存在
				UserService userService = new UserServiceImpl();
				Customer user = userService.queryByName(usname);
				if (user != null) { // 用户存在
					 result = "用户名已存在";
				} else {
					 result = "用户名可用";
				}
				
				//回传给前端
				response.getWriter().write(result);
				
				return null;
			}

	
	//登录
	public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获得请求参数信息
		Map<String, String[]> parameterMap = request.getParameterMap();	
		//创建User对象
		Customer customer = CustomBeanUtils.populate(Customer.class, parameterMap);
		//自动登录
		String autologin = request.getParameter("autologin");
		
		//调用service层进行登录判断
		UserService userService = new UserServiceImpl();
		Customer loginUser = userService.login(customer);
		
		if (loginUser != null) { //登录成功
			if (autologin != null) { //自动登录
				//创建Cookie
				Cookie cookie = new Cookie("cid", loginUser.getcCardID());
				//设置Cookie时效及作业范围
				cookie.setMaxAge(30); //单位是秒
				cookie.setPath(request.getContextPath()); //整个应用有效
				//传递给客户端
				response.addCookie(cookie);
			}
			request.getSession().setAttribute("user", loginUser);
			response.sendRedirect(request.getContextPath() + "/HomeServlet?action=ui");
		} else { //登录失败
			response.sendRedirect(request.getContextPath() + "/UserServlet?action=loginUi");
		}
		return null;
	}
		
	//退出登录
	public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("logout...");
		
		//移除cookie
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("usid")) {
					cookie.setMaxAge(0); //移除cookie
				}
			}
		}
		
		//移除session
		request.getSession().removeAttribute("user");
		response.sendRedirect(request.getContextPath() + "/HomeServlet?action=ui");
		return null;
	}
			
	

}
